package com.whcoding.test.common.validator;

import com.whcoding.test.annotion.IsMobile;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @program: spring-boot-learning
 * @description:验证手机号正则
 * @author: whcoding
 * @create: 2022-08-01 11:46
 **/
public class IsMobileValidator implements ConstraintValidator<IsMobile,String> {

	private boolean required = false;

	@Override
	public void initialize(IsMobile constraintAnnotation) {
		required = constraintAnnotation.required();
	}

	@Override
	public boolean isValid(String value, ConstraintValidatorContext context) {
		if(required){
			return ValidatorRegExUtil.isMobile(value);
		}else{
			if(StringUtils.isEmpty(value)){
				return true;
			}else {
				return ValidatorRegExUtil.isMobile(value);
			}
		}

	}
}

